/*###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.9
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############*/

var JFX_Base = function(){
    this._hashRegistry = Array();
    this._showLog = false; // change to true to enable debugging
    if(this._showLog) this._logObject = new JFX_Log('JFX JavaScript debugging log');
    this.ajaxUrl = JFXConfig.ajaxUrl;
    this.adminUrl = JFXConfig.adminUrl;
    this.lang = JFXConfig.lang;
    this.checkLoginInterval = null;
    this.events = Array();
}

JFX_Base.prototype.regenerateKeys = function(arr, type){
    if(!type) type = 'object';
    var count = 0;
    var newArr = Array();
    for(var i=0; i<arr.length; i++){
        if(typeof(arr[i])==type){
            newArr[count] = arr[i];
            count++;
        }
    }
    arr = newArr;
}
/**
 * Display a log message if javascript logging is enabled
 */
JFX_Base.prototype.log = function(msg){
    if(!this._showLog) return;
    this._logObject.step(msg);
}

JFX_Base.prototype.trigg = function(eventName, params){
    
    for(var i=0; i<this.events.length; i++){
        if(this.events[i].eventName == eventName){
            this.events[i].theFunction(params);
        }
    }
}

JFX_Base.prototype.removeEventListener = function(eventName){

    for(var i=0; i<this.events; i++){
        if(this.events[i].eventName == eventName){
            this.events[i].theFunction = function(){};
            return;
        }
    }
}

JFX_Base.prototype.registerEventListener = function(eventName, theFunction){
    
    for(var i=0; i<this.events; i++){
        if(this.events[i].eventName == eventName){
            this.events[i].theFunction = theFunction;
            return;
        }
    }
    var newEvent = new Object;
    newEvent.eventName = eventName;
    newEvent.theFunction = theFunction;
    this.events[this.events.length] = newEvent;
}

JFX_Base.prototype.checkLoginStatus = function(){
    $.getJSON(this.ajaxUrl, {'get-login-status' : 1}, JFXBase.parseLoginStatus);
}
JFX_Base.prototype.parseLoginStatus = function(json){
    if(json.status == 'ok') return;
    else{
        JFXBase.forceLogin();
    }
}


JFX_Base.prototype.forceLogin = function(){
    tb_show('Login Expired', this.adminUrl+'/admin-login/?force-login=1&KeepThis=true&TB_iframe=true&height=500&width=700', false);
}



/**
 * Find the value of pixels from the left edge of window to left edge of oElement
 */
JFX_Base.prototype.getX = function(oElement){
    var iReturnValue = 0;
    while( oElement != null ) {
        iReturnValue += oElement.offsetLeft;
        oElement = oElement.offsetParent;
    }
    return iReturnValue;
}

/**
 * Find the value of pixels from the top edge of window to top edge of oElement
 */
JFX_Base.prototype.getY = function(oElement){
    var iReturnValue = 0;
    while( oElement != null ) {
        iReturnValue += oElement.offsetTop;
        oElement = oElement.offsetParent;
    }
    return iReturnValue;
}

/**
 * Set a value for an entry in the object's registry
 */
JFX_Base.prototype.setValue = function(id, val){
    var key = this.getKey(id);
    this._hashRegistry[key].id = id;
    this._hashRegistry[key].val = val;
}

/**
 * Get the value of an entry in the object's registry
 */
JFX_Base.prototype.getValue = function(id, alt){
    for(var i=0; i<this._hashRegistry.length; i++){
        if(this._hashRegistry[i].id == id) return this._hashRegistry[i].val;
    }
    return alt;
}
/**
 * Is our value in the object's registry?
 */
JFX_Base.prototype.hasValue = function(id){
    for(var i=0; i<this._hashRegistry.length; i++){
        if(this._hashRegistry[i].id == id) return true;
    }
    return false;
}

/**
 * Get the keyname to use with this.getValue() when the id is known
 */
JFX_Base.prototype.getKey = function(id){
    for(var i=0; i<this._hashRegistry.length; i++){
        if(this._hashRegistry[i].id && this._hashRegistry[i].id == id) return i;
    }
    var key = this._hashRegistry.length;
    this._hashRegistry[key] = new Object();
    return key;
}








